package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.dtos.PageResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.common.util.BeanHelper;
import com.heima.model.admin.dtos.SensitiveDto;
import com.heima.wemedia.entity.WmSensitive;
import com.heima.wemedia.mapper.WmSensitiveMapper;
import com.heima.wemedia.service.WmSensitiveService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 敏感词信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2022-04-27
 */
@Service
public class WmSensitiveServiceImpl extends ServiceImpl<WmSensitiveMapper, WmSensitive> implements WmSensitiveService {

    /**
     * 查询敏感词列表
     *
     * @param dto
     * @return
     */
    public PageResult<SensitiveDto> findByPage(SensitiveDto dto) {
        // 构造分页条件
        IPage<WmSensitive> iPage = new Page<>(dto.getPage(),dto.getSize());

        IPage<WmSensitive> sensitiveIPage = baseMapper.selectPage(iPage, null);

        List<WmSensitive> records = sensitiveIPage.getRecords();

        List<SensitiveDto> sensitiveDtoList = BeanHelper.copyWithCollection(records, SensitiveDto.class);

        return new PageResult<SensitiveDto>(dto.getPage(),dto.getSize(),sensitiveIPage.getTotal(),sensitiveDtoList);
    }

    /**
     * 添加敏感词
     *
     * @param sensitiveDto
     * @return
     */
    public boolean saveSensitive(SensitiveDto sensitiveDto) {
        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmSensitive::getSensitives,sensitiveDto.getSensitives());


        WmSensitive wmSensitive = getOne(queryWrapper);

        if (wmSensitive != null) {
            throw new LeadException(AppHttpCodeEnum.DATA_EXIST);
        }

        WmSensitive sensitive = new WmSensitive();
        sensitive.setSensitives(sensitiveDto.getSensitives());

        long l = System.currentTimeMillis();
        sensitive.setCreatedTime(new Date(l));

        boolean b = save(sensitive);

        return b;
    }

    /**
     * 修改敏感词
     *
     * @param dto
     * @return
     */
    public boolean updateByIdSensitive(SensitiveDto dto) {
        WmSensitive wmSensitive = new WmSensitive();
        wmSensitive.setSensitives(dto.getSensitives());
        wmSensitive.setId(dto.getId());
        boolean b = updateById(wmSensitive);
        return b;
    }

    /**
     * 删除敏感词
     *
     * @param
     * @return
     */
    public boolean deleteById( Integer id) {

        LambdaQueryWrapper<WmSensitive> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(WmSensitive::getId,id);
        boolean b = remove(queryWrapper);

        return b;
    }
}
